package com.ztgf.shopping.service.shop;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.ztgf.commodity.models.dto.BrandInfoAuditDTO;
import com.ztgf.commodity.models.dto.BrandInfoDTO;
import com.ztgf.commodity.models.service.IBrandInfoAuditService;
import com.ztgf.commodity.models.service.IBrandInfoService;
import com.ztgf.shopping.models.dto.shop.ShopQualificationAuditDTO;
import com.ztgf.shopping.models.vo.shop.ShopBrandRelationAuditVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.ztgf.shopping.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ztgf.shopping.mapper.shop.ShopBrandRelationAuditMapper;
import com.ztgf.shopping.models.service.shop.IShopBrandRelationAuditService;
import com.ztgf.shopping.models.bo.shop.ShopBrandRelationAuditSaveBO;
import com.ztgf.shopping.models.dto.shop.ShopBrandRelationAuditDTO;

import java.util.ArrayList;
import java.util.List;

/**
 * 店铺品牌关系审核表控制器
 *
 * @author zhanghualang
 * @Date 2020-06-24 15:38:05
 */
@DubboService
public class ShopBrandRelationAuditServiceImpl extends ServiceImpl<ShopBrandRelationAuditMapper, ShopBrandRelationAuditDTO> implements IShopBrandRelationAuditService{

    @Resource
    private YmlConfig ymlConfig;
    @DubboReference
    private IBrandInfoService iBrandInfoService;
    @DubboReference
    private IBrandInfoAuditService iBrandInfoAuditService;

    @Override
    public boolean add(ShopBrandRelationAuditSaveBO saveBO) {
        ShopBrandRelationAuditDTO dto = new ShopBrandRelationAuditDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    public List<ShopBrandRelationAuditDTO> queryListByShopAudit(Long shopAuditId, Integer status, Integer type) {
        Wrapper<ShopBrandRelationAuditDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("shop_audit_id", shopAuditId).eq("status", status).eq("type", type);
        return this.selectList(wrapper);
    }

    @Override
    public List<ShopBrandRelationAuditVO> queryCategoryList(Long shopId) {
        ArrayList<ShopBrandRelationAuditVO> vos = new ArrayList<ShopBrandRelationAuditVO>();
        Wrapper<ShopBrandRelationAuditDTO> wrapper = new EntityWrapper<>();
        //0注册，1开店，2资质变更，3新增品牌，4新增类目
        wrapper.eq("shop_id", shopId).eq("status", 0);
        wrapper.andNew().eq("type", 3).or().eq("type", 4);
        List<ShopBrandRelationAuditDTO> dtos = this.selectList(wrapper);
        //品牌是否存在 0存在。1不存在 默认0
        for (ShopBrandRelationAuditDTO dto : dtos){
            ShopBrandRelationAuditVO vo = new ShopBrandRelationAuditVO();
            BeanUtils.copyProperties(dto,vo);
            if (dto.getBrandType()==0){
                BrandInfoDTO brandInfoDTO = iBrandInfoService.selectById(dto.getBrandId());
                vo.setBrandName(brandInfoDTO.getName());
                vo.setLogoUrl(brandInfoDTO.getLogoUrl());
            }else{
                BrandInfoAuditDTO brandInfoAuditDTO = iBrandInfoAuditService.selectById(dto.getBrandId());
                vo.setBrandName(brandInfoAuditDTO.getName());
                vo.setLogoUrl(brandInfoAuditDTO.getLogoUrl());
            }

            vos.add(vo);
        }

        return vos;
    }
}
